/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Negocio;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.Cell;
import jxl.CellView;
import jxl.Workbook;
import jxl.format.CellFormat;
import jxl.write.Number;
import jxl.write.*;

/**
 *
 * @author jeferson
 */
public class Excel {
    
    private WritableWorkbook wb;
    private String filename;
    
    public Excel(String filename){
        try {
            this.filename = filename;
            wb = Workbook.createWorkbook(new File(filename));
        } catch (IOException ex) {
            Logger.getLogger(Excel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public WritableSheet addPlanilha(String title, int pos){
        WritableSheet sheet = wb.createSheet(title, pos);
        return sheet;
    }
    
    public void addLabel(int planilha, String text, int l, int c, int w, jxl.format.Alignment align, jxl.format.Colour color){
        try {
            WritableSheet s = wb.getSheet(planilha);
            WritableCellFormat wcf = new WritableCellFormat();
            wcf.setAlignment(align);
            wcf.setBackground(color);
            wcf.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.MEDIUM, jxl.format.Colour.BLACK);
            s.setColumnView(c, w);
            if (s != null) {
                try {
                    Label label = new Label(c, l, text, wcf);
                    s.addCell(label);
                } catch (WriteException ex) {
                    Label label = new Label(c, l, "", wcf);
                    s.addCell(label);
                }
            }
        } catch (WriteException ex) {
            Logger.getLogger(Excel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void addNumber(int planilha, Double d, int l, int c, int w, jxl.format.Alignment align, jxl.format.Colour color){
        try {
            WritableSheet s = wb.getSheet(planilha);
            WritableCellFormat wcf = new WritableCellFormat();
            wcf.setAlignment(align);
            wcf.setBackground(color);
            wcf.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.MEDIUM, jxl.format.Colour.BLACK);
            CellView cv = new CellView();
            cv.setSize(w);
            cv.setFormat(wcf);
            s.setColumnView(c, w);
            if (s != null) {
                try {
                    Number number = new Number(c, l, d, wcf);                 
                    s.addCell(number);
                } catch (WriteException ex) {
                    Label label = new Label(c, l, "", wcf);
                    s.addCell(label);
                }
            }
        } catch (WriteException ex) {
            Logger.getLogger(Excel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void save(){
        try {
            wb.write();
            wb.close();
        } catch (WriteException ex) {
            Logger.getLogger(Excel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Excel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
